package com.kingyun.gpsinspection.purificationservice.services.tdm.innerservice.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.MessageImpartEmployeeMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.MessageImpartMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.MessageImpart;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.MessageImpartEmployee;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.MessageImpartVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.MessageImpartService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liyi on 2017/3/22.
 */
public class MessageImpartServiceImpl implements MessageImpartService {
    @Autowired
    public MessageImpartMapper messageImpartMapper;
    @Autowired
    public MessageImpartEmployeeMapper messageImpartEmployeeMapper;

    /**
     * 查询消息信息
     * @param messageImpart
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> list(MessageImpartVo messageImpart) throws Exception {
        Map<String, Object> map = new HashMap<>();
        messageImpart.setLimit( messageImpart.getLimit() == null ? 10 : messageImpart.getLimit() );
        messageImpart.setOffset( messageImpart.getOffset() == null ? 0 : messageImpart.getOffset() );
        map.put("total",messageImpartMapper.queryCount(messageImpart));
        map.put("rows", JSON.toJSON(messageImpartMapper.queryList(messageImpart)));
        return map;
    }

    /**
     * 查询消息信息返回json
     * @param messageImpart
     * @return
     */
    @Override
    public JSONArray loadMessageImpart(MessageImpart messageImpart) {
        /*装载查询条件*/
       /* messageImpart.setLimit(null == messageImpart.getLimit() ? 10 : messageImpart.getLimit());
        messageImpart.setOffset(null == messageImpart.getOffset() ? 10 : messageImpart.getOffset());*/
        // 查询消息告知信息
        List<MessageImpartVo> baseRows = messageImpartMapper.querySelectList(messageImpart);
        // Json格式的数组形式
        JSONArray array = new JSONArray();
        //json格式的单个对象形式
        JSONObject obj;
        for (int i = 0; i <baseRows.size() ; i++) {
            obj = new JSONObject();
            // 设置编号
            obj.put("num", i);
            // 设置告知人员id
            obj.put("id", baseRows.get(i).getMessageImpartEmployeeId());
            // 设置是否已读
            obj.put("haveRead", (baseRows.get(i).getReadStatus()==1)?false:true);
            // 设置告知内容
            obj.put("message", baseRows.get(i).getMessageContent());
            array.add(obj);
        }
        return array;
    }

    /**
     * 保存信息
     * @param messageImpart
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int save(MessageImpartVo messageImpart) {
        int a = 0;
        // 判断消息告知是否为空
        if (messageImpart != null) {
            // 设置ID
            messageImpart.setMessageImpartId(UniqueUtil.uuid());
            // 设置消息时间
            messageImpart.setMessageTime(new Date());
            // 设置消息状态
            messageImpart.setMessageStatus((short) 1);
            // 保存消息告知信息
            a +=messageImpartMapper.insertSelective(messageImpart);
            // 保存消息告知和人员的关系表：
            if (messageImpart.getUserId() != null) {
                String[] array = messageImpart.getUserId().split(",");
                if (array != null && array.length > 0) {
                    for (int i = 0; i < array.length; i++) {
                        // 判断人员id是否为空
                        if (StringUtils.isNotBlank(array[i])) {
                            // 创建消息告知人员信息
                            MessageImpartEmployee messageImpartEmployee = new MessageImpartEmployee();
                            // 设置ID
                            messageImpartEmployee.setMessageImpartEmployeeId(UniqueUtil.uuid());
                            // 设置消息告知ID
                            messageImpartEmployee.setMessageImpartId(messageImpart.getMessageImpartId());
                            // 设置人员ID
                            messageImpartEmployee.setEmployeeId(array[i]);
                            // 设置读取状态
                            messageImpartEmployee.setReadStatus((short) 1);
                            // 保存信息
                            a += messageImpartEmployeeMapper.insert(messageImpartEmployee);
                        }
                    }
                }

            }
        }
        return a;
    }

    /**
     * 根据消息告知人员关系id修改读取状态
     * @param id
     * @return
     */
    @Override
    public int updateReadStatusById(String id) {
        return messageImpartEmployeeMapper.updateReadStatusById(id);
    }
    /**
     * 根据任务id修改读取状态
     * @param id
     * @return
     */
    @Override
    public int updateMessageStatusById(String id) {
        return messageImpartMapper.updateMessageStatusById(id);
    }
}
